home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / amiga / stack.a < prev    next >
Text File  |  1994-02-01  |  3KB  |  82 lines

  1.  
  2.         ;   STACK.A
  3.         ;
  4.         ;   (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  5.         ;
  6.         ;   STACK CHECKING CODE.  This code is guarenteed to not
  7.         ;   destroy any registers other then munging A5 and A7
  8.         ;   as appropriate.
  9.  
  10.         section text,code
  11.  
  12.         xdef    __stk_alloc
  13.         xdef    __stk_free
  14.         xref    __stk_base
  15.         xref    __CStackAlloc
  16.         xref    __CStackFree
  17.  
  18.         ;   __stk_alloc called if we ran out of stack!    This
  19.         ;   is called with two stack-based arguments (we can't use
  20.         ;   registers because we can't destroy any registers other
  21.         ;   then fixing up A5 and A7).
  22.         ;
  23.         ;   The two arguments are
  24.         ;    8(sp)    #of bytes of arguments to procedure by caller
  25.         ;    4(sp)    #of bytes of saved registers by callee
  26.         ;        (not including RTS vector or FP)
  27.         ;
  28.         ;   We must return with no registers munged other then A5
  29.         ;   and A7.  A5 must be the new stack pointer and A7 must
  30.         ;   be the new frame pointer, exchanged because on return
  31.         ;   we will fall through to an EXG instruction.
  32.         ;
  33.         ;   On call, A5 will contain what the caller had attempted
  34.         ;   to allocate.  The amount is calculated A7-A5+8, but
  35.         ;   after our movem it becomes A7-A5+8+16
  36.  
  37. __stk_alloc    movem.l D0-D1/A0-A1,-(sp)
  38.         move.l    A7,A1
  39.         sub.l    A5,A1
  40.         pea    24(A1)            ; space procedure needs
  41.         pea    24(sp)            ; pointer to control frame
  42.         jsr    __CStackAlloc(pc)
  43.         addq.l    #4,sp
  44.  
  45.         ; new stack pointer returned in D0, does not include space
  46.         ; allocated by the callee.  That space can be popped off
  47.         ; the old stack and used to setup A7 and A5 for our return.
  48.         ;
  49.         ; note that we must setup A5 and A7 exchanged since we fall
  50.         ; through to an EXG instruction on return.
  51.  
  52.         move.l    sp,A0            ; ptr for final register restore
  53.         move.l    D0,sp            ; SP = new A5 frame pointer
  54.         sub.l    (A0)+,D0
  55.         move.l    D0,A5            ; A5 = new A7 stack pointer
  56.         move.l    16(A0),-(sp)        ; temp copy our return vector
  57.         movem.l (A0),D0-D1/A0-A1    ; restore registers
  58.         rts                ; return
  59.  
  60.  
  61.  
  62.         ;   __stk_free()   - the caller's return vector was replaced
  63.         ;             with __stk_free and it's return FP
  64.         ;             replaced with a pointer to the old stack
  65.         ;             frame's return FP (after copying the
  66.         ;             old stack frame's return FP up past the
  67.         ;             callee's registers)
  68.         ;
  69.         ;             We must restore the original stack,
  70.         ;             original FP, and return without
  71.         ;             modifying anything else.
  72.  
  73. __stk_free    move.l    A5,A7            ; move to original stack
  74.         move.l    (sp)+,A5        ; restore original saved FP
  75.         movem.l D0-D1/A0-A1,-(sp)   ; save scratch regs! + dummy A2
  76.         jsr    __CStackFree(pc)    ; returns actual rts vector
  77.         movem.l (sp)+,D0-D1/A0-A1   ; restore scratch regs!
  78.         rts                ; blammo.
  79.  
  80.         END
  81.  
  82.